package com.loong.common.utils;



import org.springframework.util.ObjectUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Optional;

/**
 * Md5相关的操作
 */
public class Md5Utils {

    /**
     * 默认密码组合
     */
    protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };


    /***
     * md5 加密方法
     *
     * @param s
     *            加密的参数
     * @return 加密后返回的字符串
     */
    public final static String md5(String s) {
        try {
            byte[] strTemp = s.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 对文件进行加密
     * 生成32位
     *
     * @param file
     * @return md5原串
     */
    public static String Md5String(File file) {
        try {
            return hexString(digest(file));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String Md5Base64(File file) {
        try {
            return hexBase64(digest(file));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] digest(File file) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
            int readCount = 0;
            byte[] buff = new byte[2048];
            while ((readCount = inputStream.read(buff)) > 0) {
                messageDigest.update(buff, 0, readCount);
            }
            return messageDigest.digest();
        } catch (Exception e) {
            return null;
        } finally {
            Optional.ofNullable(messageDigest).ifPresent(digest -> digest.reset());
        }
    }

    /**
     * 转换digest成String
     *
     * @param digest
     * @return
     */
    public static String hexString(byte[] digest) {
        if(ObjectUtils.isEmpty(digest)){
            return null;
        }
        final StringBuilder sb = new StringBuilder();
        for (byte t : digest) {
            //
            String s = Integer.toHexString(t & 0xFF);
            if (s.length() == 1) {
                s = 0 + s;
            }
            sb.append(s);
        }
        return sb.toString();
    }

    /**
     * 转成base64
     *
     * @param digest
     * @return
     */
    public static String hexBase64(byte[] digest) {
        if(ObjectUtils.isEmpty(digest)){
            return null;
        }
        return Base64.getEncoder().encodeToString(digest);
    }
    // todo 速度测试
    private static void hexString(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 0xf0) >> 4];
        char c1 = hexDigits[bt & 0xf];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }
    /**
     *
     */
}
